package com.campus.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.activity.entity.Activity;
import com.campus.activity.mapper.ActivityMapper;
import com.campus.activity.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 活动服务实现类
 * 
 * @author System
 * @since 2024
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public boolean addActivity(Activity activity) {
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activity.setDeleted(0);
        return save(activity);
    }

    @Override
    public boolean updateActivity(Integer activityId, Activity activity) {
        activity.setActivityId(activityId);
        activity.setUpdateTime(LocalDateTime.now());
        return updateById(activity);
    }

    @Override
    public boolean deleteActivity(Integer activityId) {
        Activity activity = new Activity();
        activity.setActivityId(activityId);
        activity.setDeleted(1);
        activity.setUpdateTime(LocalDateTime.now());
        return updateById(activity);
    }

    @Override
    public List<Activity> getActivitiesByType(String activityType) {
        return activityMapper.selectByActivityType(activityType);
    }

    @Override
    public List<Activity> getActivitiesByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
        return activityMapper.selectByTimeRange(startTime, endTime);
    }

    @Override
    public List<Activity> getActivitiesByLocation(String location) {
        return activityMapper.selectByLocation(location);
    }

    @Override
    public List<Activity> getUpcomingDeadlineActivities() {
        return activityMapper.selectUpcomingDeadline(LocalDateTime.now());
    }

    @Override
    public List<Activity> getActivitiesByOrganizerId(Integer organizerId) {
        return activityMapper.selectByOrganizerId(organizerId);
    }

    @Override
    public boolean publishActivity(Activity activity) {
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activity.setDeleted(0);
        return save(activity);
    }

    @Override
    public List<Activity> getAllOpenActivities() {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0)
                   .gt("registration_deadline", LocalDateTime.now())
                   .orderByAsc("start_time");
        return list(queryWrapper);
    }

    @Override
    public List<Activity> queryActivities(String activityType, LocalDateTime startTime, LocalDateTime endTime, String location) {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        
        if (StringUtils.hasText(activityType)) {
            queryWrapper.eq("activity_type", activityType);
        }
        
        if (startTime != null) {
            queryWrapper.ge("start_time", startTime);
        }
        
        if (endTime != null) {
            queryWrapper.le("end_time", endTime);
        }
        
        if (StringUtils.hasText(location)) {
            queryWrapper.like("location", location);
        }
        
        queryWrapper.orderByAsc("start_time");
        return list(queryWrapper);
    }

    @Override
    public List<Activity> getPopularActivities() {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0)
                   .eq("status", "PUBLISHED")
                   .orderByDesc("participant_count")
                   .orderByDesc("create_time")
                   .last("LIMIT 10");
        return list(queryWrapper);
    }

    @Override
    public List<Activity> getLatestActivities(Integer limit) {
        if (limit == null || limit <= 0) {
            limit = 5;
        }
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0)
                   .eq("status", "PUBLISHED")
                   .orderByDesc("create_time")
                   .last("LIMIT " + limit);
        return list(queryWrapper);
    }
}